package com.efounder.form.model;

import com.efounder.builder.base.data.EFDataSet;
import com.efounder.builder.base.data.EFRowSet;
import com.efounder.builder.base.json.JSONUtil;
import com.efounder.form.DMComponent;
import com.efounder.form.DataSetComponent;
import com.efounder.form.base.DataComponent;
import com.efounder.form.base.IComponent;
import com.efounder.mdm.EFMDMDataModel;
import com.efounder.service.IService;
import com.efounder.util.AppContext;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Javor on 16/9/13.
 */

public class MDModel extends DataComponent implements IComponent, DataSetComponent {

    private String id;
    private List<DMComponent> dmCompList;

    private EFMDMDataModel mdmDataModel = new EFMDMDataModel();

    private IService saveService;
    private IService loadService;
    private String DCT_ID;

    public MDModel(){
//        EFDataSet ds = new EFDataSet();
//        ds.setTableName("CPY");
//
//        EFRowSet rs1 = new EFRowSet();
//        rs1.put("QYBH","93");
//        rs1.put("QYMC","张超");
//        ds.insertRowSet(rs1);
//
//        EFRowSet rs2 = new EFRowSet();
//        rs2.put("QYBH","95");
//        rs2.put("QYMC","李鹏");
//        ds.insertRowSet(rs2);
//
//        EFRowSet rs3 = new EFRowSet();
//        rs3.put("QYBH","97");
//        rs3.put("QYMC","宋涛");
//        ds.insertRowSet(rs3);
//        this.mdmDataModel.setDataSet("CPY", ds);
//
//
//        ds = new EFDataSet();
//        ds.setTableName("project_dic");
//
//        rs1 = new EFRowSet();
//        rs1.put("QYBH","93");
//        rs1.put("QYMC","中铁十局汉十高铁项目部");
//        ds.insertRowSet(rs1);
//
//        rs2 = new EFRowSet();
//        rs2.put("QYBH","95");
//        rs2.put("QYMC","中铁十局济南项目部");
//        ds.insertRowSet(rs2);
//
//        rs3 = new EFRowSet();
//        rs3.put("QYBH","97");
//        rs3.put("QYMC","中铁十局济青高速项目部");
//        ds.insertRowSet(rs3);
//        this.mdmDataModel.setDataSet("project_dic", ds);
    }

    @Override
    public void creationComplete() {

        new Thread(){

            @Override
            public void run() {
                super.run();

                setActiveDataSet(mdmDataModel.getDataSet(DCT_ID));

                if (loadService == null) return;
                EFRowSet rowSet = (EFRowSet)loadService.executeService(null,null,null,null,null);
                if (rowSet == null) return;
                mdmDataModel.setDataSetMap(rowSet.getDataSetMap());

                HashMap<String, EFDataSet> map = mdmDataModel.getDataSetMap();
                if (map == null) return;
                for (String key:map.keySet()){
                    setActiveDataSet(map.get(key));
                    break;
                }

            }
        }.start();


    }

    public void setActiveDataSet(final EFDataSet dataSet) {
        if (dataSet == null) return;
        if (dmCompList == null || dmCompList.size() == 0) return;
        //if (dataSet.getTableName().equals(comp.getDataSetID())) {
//}
        for (final DMComponent comp : dmCompList)
            AppContext.getCurrentActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    comp.setDataSet(dataSet);
                }
            });
    }

    /**
     * 将模型转成Datajson
     * @return
     */
    public String convert2DataJSON() {
        return JSONUtil.RowSet2JSON(mdmDataModel, true);
    }

    public void toModel(String dataJSON, String extJSON) {
        mdmDataModel.setDataSet(DCT_ID, JSONUtil.JSON2DataSet(dataJSON));
        this.setActiveDataSet(mdmDataModel.getDataSet(DCT_ID));
    }

    @Override
    public List<DMComponent> getDmCompList() {
        return dmCompList;
    }

    @Override
    public void setDmCompList(List<DMComponent> value) {
        this.dmCompList= value;
    }

    @Override
    public EFDataSet getDataSet(String dataSetKey) {
        return null;
    }

    @Override
    public List<String> getDataSetKey() {
        return null;
    }

    @Override
    public List<String> getDOMetaData(String obj_id) {
        return null;
    }

    @Override
    public void insertDMComponent(DMComponent dmComponent) {
        if(dmCompList == null){
            dmCompList = new ArrayList<DMComponent>();
        }
        dmCompList.add(dmComponent);
    }

    @Override
    public void removeDMComponent(DMComponent dmComponent) {

    }

    @Override
    public Object loadByParam(Object param, Object userObject) {
        return null;
    }

    public String getDCT_ID() {
        return DCT_ID;
    }

    public void setDCT_ID(String DCT_ID) {
        this.DCT_ID = DCT_ID;
    }

    public EFMDMDataModel getMdmDataModel() {
        return mdmDataModel;
    }

    public void setMdmDataModel(EFMDMDataModel mdmDataModel) {
        this.mdmDataModel = mdmDataModel;
    }

    public IService getSaveService() {
        return saveService;
    }

    public void setSaveService(IService saveService) {
        this.saveService = saveService;
    }

    public IService getLoadService() {
        return loadService;
    }

    public void setLoadService(IService loadService) {
        this.loadService = loadService;
    }
}
